var pageMod = require("page-mod");
var widget = require("widget");
var panel = require("panel");
var tabs = require("tabs");
var data = require("self").data;
var cm = require("context-menu");
var request = require("request");
var data = require("self").data;

exports.main = function(options, callbacks) {
    console.log(options.loadReason);
    //var HTML = "<html><h2>Change Username</h2><p>Default new password is the same as username.<b>Warning: specify username in 'a'..'z','A'..'Z','0'..'9', other characters may not be supported!!!</b></p><form>User: <input type='text' name='user' id='user'/><input type='button' value='OK' name='ok' onClick='self.postMessage(document.getElementById("+'"user"'+").value);'/></form></html>";
    //console.log(HTML);
    /*var myPanel = panel.Panel({
        width:500,
        height:200,
        contentScript: "self.port.on('showing', function(HTML) {document.body.innerHTML = HTML;});",
	onMessage: function() {
	}
    });
    widget.Widget({
        id: "routerconf",
        label: "Router Configure",
        contentURL: "http://www.mozilla.org/favicon.ico",
        panel: myPanel,
        onClick: function() {
            myPanel.port.emit('showing', HTML);
            myPanel.show();
        },
    });
    */
    pageMod.PageMod({
        include: "http://192.168.1.1/main.html",
        contentScriptWhen: 'start',
        contentScript: 'document.cookie = "Name=0telecomadmin";',
    });
    cm.Item({
        label: "Change Username and Password",
        context: cm.URLContext("http://192.168.1.1/*"),
        contentScript: 'self.on("click", function() {var name="admin";var passwd="admin";self.postMessage([name,passwd]);});',
        onMessage:function (text) {
            console.log('usrUserName='+text[0]+'&usrPassword='+text[1]);
            var req = request.Request({
                url: 'http://192.168.1.1/password.cgi?usrUserName='+text[0]+'&usrPassword='+text[1],
                content: {
                    Referer: 'http://192.168.1.1/password.html',
                    Cookie: 'Name=0telecomadmin',
                },
            });
            req.get();
        },
    });
};

exports.onUnload = function (reason) {
    console.log(reason);
};


